//1.	Load a single line from a text file into a dynamically allocated array. (setting up a programming environment is implied, and not part of this primer)
//a.	For information on how to load data from a file, see http://www.cplusplus.com/reference/iostream/istream/get.html
//b.	For information on how to print to the console using printf, see http://www.cplusplus.com/reference/clibrary/cstdio/printf.html
//c.	For information on how to dynamically allocate memory in c++, see http://www.fredosaurus.com/notes-cpp/newdelete/50dynamalloc.html

//#define PRIMER1

#ifdef PRIMER1

#include <iostream>
#include <fstream>

#define NUM_CHARS	256
#define MAX_DATA	256

int main()
{
	char buffer[NUM_CHARS], c;
	std::ifstream myFile;
	int length = 0;

	char * myData;

	std::cout << "Enter the name of the text file" << std::endl;

	std::cin.get(buffer, NUM_CHARS);

	myFile.open(buffer);

	if(myFile.good())
	{
		// create a dynamically allocated array
		myData = new char[MAX_DATA];
		int i = 0;

		while(myFile.good() && i < MAX_DATA)
		{
			c = myFile.get(); // gets a character from the file

			if(myFile.good())
			{
				if (c != '\0' || c != '\n')
					myData[i] = c;
				else
					break;
			}
			++i;
		}

		length = i - 1;

	}
	else
	{
		std::cout << "Error: File corrupted or does not exist" << std::endl;
		return 0;
	}

	printf("Here is the data we read:\n");
	for (int i = 0; i < length; ++i)
	{
		if (i > 0)
			printf(", ");
		printf("%c", myData[i]);
	}

	printf("\n");

	myFile.close();

	return 0;
}

#endif